# encoding: utf-8
# @File  : refresh_token.py
# @Author: shaoyun
# @Date  :  2025/06/01
from datetime import datetime
from sqlalchemy import Column, String, DateTime, ForeignKey, Boolean
from sqlalchemy.orm import relationship

from app.db.mysql import Base


class RefreshToken(Base):
    """Refresh Token 模型"""
    __tablename__ = "refresh_tokens"

    id = Column(String(64), primary_key=True, index=True)
    token = Column(String(256), unique=True, index=True, nullable=False)
    user_id = Column(String(36), ForeignKey("users.id"), nullable=False)
    expires_at = Column(DateTime, nullable=False)
    is_revoked = Column(Boolean, default=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    last_used_at = Column(DateTime, nullable=True)

    # 关联用户
    user = relationship("User", back_populates="refresh_tokens")